﻿#include "BrowserTableHandlerFESensors.h"
#include "OperatorsInterface/OperAbstractSubsystem.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSensors.h"
#include "FITK_Component/FITKAcousticsModel/FITKAbstractFESubSys.h"

namespace GUI
{
    void BrowserTableHandlerFESensors::updateTable()
    {
        if (_tableWidget == nullptr) return;
        _tableWidget->clear();
        _tableWidget->setColumnCount(0);
        _tableWidget->setRowCount(0);

        //设置表头
        QStringList headerLabels;
        headerLabels << tr("Name") << tr("Connection") << tr("Node")
            << tr("Element") << tr("Result Type") << tr("Notes");
        _tableWidget->setColumnCount(headerLabels.size());
        _tableWidget->setHorizontalHeaderLabels(headerLabels);
        //更新表格
        this->updateData();
    }

    void BrowserTableHandlerFESensors::updateData()
    {
        if (_tableWidget == nullptr|| _sensorMgr == nullptr) return;

        const int n = _sensorMgr->getDataCount();
        _tableWidget->setRowCount(n);
        for (int i = 0; i < n; ++i)
        {
            Acoustics::FITKAcousticsSensors* dataSensorData = _sensorMgr->getDataByIndex(i);
            if (!dataSensorData) continue;

            //数据ID
            const int id = dataSensorData->getDataObjectID();

            Acoustics::FITKAbstractFESubSys* feSubSys = dataSensorData->getFESubSysObj();
            if (!feSubSys)continue;
            QPair<int, int> nodeElemID = dataSensorData->getConnection();
            Acoustics::FITKAcousticsSensors::SensorResultType resultType = dataSensorData->getSensorResultType();
            QString type{};
            switch (resultType)
            {
            case Acoustics::FITKAcousticsSensors::SensorResultVelocityMagnitude:
                type = "Velocity [magnitude]"; break;
            case Acoustics::FITKAcousticsSensors::SensorResultVelocityDirectional:
                type = "Velocity [directional]"; break;
            case Acoustics::FITKAcousticsSensors::SensorResultPressure:
                type = "Pressure"; break;
            }
            if (type.isEmpty())continue;

            _tableWidget->setItem(i, 0, new QTableWidgetItem(dataSensorData->getDataObjectName(), id));
            _tableWidget->setItem(i, 1, new QTableWidgetItem(feSubSys->getDataObjectName(), id));
            _tableWidget->setItem(i, 2, new QTableWidgetItem(QString::number(nodeElemID.first), id));
            _tableWidget->setItem(i, 3, new QTableWidgetItem(QString::number(nodeElemID.second), id));
            _tableWidget->setItem(i, 4, new QTableWidgetItem(type, id));
            _tableWidget->setItem(i, 5, new QTableWidgetItem("", id));
        }
        _tableWidget->autoResizeCellWidth();
        _tableWidget->setTableItemEditable(false);
    }

    EventOper::OperAbstractSubsystem * BrowserTableHandlerFESensors::getSubsystemOper()
    {
        //从操作器仓库获取
        return Core::FITKOperatorRepo::getInstance()->
            getOperatorT<EventOper::OperAbstractSubsystem>("OperFESensors");
    }

    bool BrowserTableHandlerFESensors::enableAttriSet()
    {
        return false;
    }
}

